package com.wedimob.slideshare.task.disk;

import java.net.URI;
import java.text.SimpleDateFormat;

import com.wedimob.slideshare.RuntimeConfig;
import com.wedimob.slideshare.bo.Author;
import com.wedimob.slideshare.task.PoolExecutor;
import com.wedimob.slideshare.task.Task;
import com.wedimob.slideshare.task.TaskType;

public class TaskAuthorWriter extends Task<Author, Author> {

    static private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public TaskAuthorWriter(PoolExecutor pool, Author input, int timeout) {
        super(TaskType.DISK, pool, input, 0, timeout);
    }

    @Override
    public void run() {
        try {
            URI uri = new URI(RuntimeConfig.getInstance().getRootpath() + "/" + PathHelper.normalizePath(input.getUsername())
                    + "/index.html");
            StringBuilder sb = new StringBuilder();
            sb.append("<html>");
            sb.append("<body>");
            sb.append("<div>");
            sb.append("<a href='");
            sb.append(input.getURL());
            sb.append("'>");
            sb.append(input.getName());
            sb.append("</a>");
            sb.append("</div>");
            sb.append(input.getTitle());
            sb.append("<br/>");
            sb.append(input.getOrganization());
            sb.append("<br/>");
            sb.append(input.getLocation());
            sb.append("<br/>");
            sb.append(input.getResume());
            sb.append("<br/>");
            sb.append(dateFormat.format(input.getRegistrationDate().getTime()));
            sb.append("<br/>");
            sb.append("<div>");
            sb.append("</div>");
            sb.append("<br/><img src='author.jpeg'/>");
            sb.append("</body>");
            sb.append("</html>");
            RuntimeConfig.getInstance().getWriter().write(sb.toString().getBytes(), uri);
        } catch (Exception e) {
            e.printStackTrace();
            this.e = e;
        }
    }
}
